using System;

namespace tvgrabau
{
	public class ArgumentParser
	{	
		private string[] arguments;
		private string configprefix = "";
		private bool called = true;
		
		public ArgumentParser(string[] a)
		{
			arguments = a;
		}
		
		public ArgumentParser(string[] a, string startdir)
		{
			arguments = a;
			this.configprefix = startdir;
		}
		
		public ApplicationArguments ParseArguments()
		{
			ApplicationArguments aa = new ApplicationArguments();
			aa.Config = configprefix + System.IO.Path.GetFileName(aa.Config);
			try
			{
				int x = 0;
				foreach (string a in arguments)
				{
					if (a == "-h" || a == "--help")
					{
						aa.ExecMode = ApplicationArguments.RunMode.ShowHelp;
					}
					if (a == "--configure")
					{
						aa.ExecMode = ApplicationArguments.RunMode.Configuration;
					}					
					if (a == "--config")
					{
						aa.Config = arguments[x + 1];
					}
					/*else
					{
						if (called)
						{
							aa.Config = configprefix + System.IO.Path.GetFileName(aa.Config);
							called = false;
						}
					}*/
					
					if (a == "--list-channels")
					{
						aa.ExecMode = ApplicationArguments.RunMode.ListChannels;
					}
					if (a == "--offset")
					{
						aa.DayOffset = int.Parse(arguments[x + 1]);
					}
					if (a == "--days")
					{
						aa.Days = int.Parse(arguments[x + 1]);
					}
					if (a == "--webwarper")
					{
						aa.WebWarper = true;
					}
					if (a == "--fast")
					{
						aa.FastMode = true;
					}
					if (a == "--quiet")
					{
						aa.Quiet = true;
					}
					if (a == "--output")
					{
						aa.OutputFilename = arguments[x + 1];
					}
					if (a == "--test")
					{
						aa.ExecMode = ApplicationArguments.RunMode.Test;
					}
					if (a == "--capabilities")
					{
						aa.XMLTV_OutputMode = ApplicationArguments.XMLTV_Mode.Capabilities;
					}
					if (a == "--description")
					{
						aa.XMLTV_OutputMode = ApplicationArguments.XMLTV_Mode.Description;
					}
					if (a == "--plugin")
					{
						aa.PluginName = arguments[x + 1];
					}
					if (a == "--version")
					{
					    aa.ExecMode = ApplicationArguments.RunMode.Version;
					}
					
					x++;
				}
			}
			catch (System.Exception ex)
			{
				Console.WriteLine("Argument parse exception occured: "+ex.Message);
				Console.WriteLine("Occured at: "+ex.Source);
				Console.WriteLine("Stack trace:");
				Console.WriteLine(ex.StackTrace);
			}
			
			return aa;
		}
		
	}
	
	public class ApplicationArguments
	{
		public enum XMLTV_Mode
		{
			None,
			Capabilities,
			Description
		}
		
		public enum RunMode
		{
			Grab,
			Test,
			ShowHelp,
			ListChannels,
			Configuration,
			Version
		}
	
		public RunMode ExecMode = RunMode.Grab;
		public string Config = "config.xml";
		public string OutputFilename = "guide.xml";
		public int Days = 7;	// Default to a week
		public int DayOffset = 0;
		public bool Quiet = false;
		public XMLTV_Mode XMLTV_OutputMode = XMLTV_Mode.None;
		public bool MinimumData = false;
		public bool Today = false;
		public bool FastMode = false;
		public bool WebWarper = false;
		public string PluginName = "";
	}
	
	
}
